<template>
  <div class="mod-config">
    <el-form size="mini" :inline="true" @keyup.enter.native="getDataList(true)">
      <el-form-item>
        <el-input clearable v-model="queryParams.queryModel.createUser" placeholder="创建人"></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryParams.queryModel.displayTime"
          type="daterange"
          align="left"
          style="width:380px"
          unlink-panels
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="dateChange"
          :picker-options="createTimePickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(true)" type="primary" icon="el-icon-search">查询</el-button>
        <el-button @click="resetParams" type="warning" icon="el-icon-delete">重置</el-button>
        <el-button v-if="isAuth('sport:sporthistorymiddle:save')" type="primary" icon="el-icon-plus" @click="addHandle()">新增</el-button>
        <el-button v-if="isAuth('sport:sporthistorymiddle:save')" type="primary" @click="excelImport()" icon="el-icon-download">导入</el-button>
        <!-- <el-button v-if="isAuth('sport:sporthistorymiddle:save')" type="primary" :loading="btnLoading" @click="excelExport()" icon="el-icon-upload2">导出</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      highlight-current-row
      border
      :height="tableHeight"
      :data="pageData.list"
      :default-sort = "{prop: 'updateTime', order: 'ascending'}"
      @sort-change="sortChange"
      :header-cell-style="headerCellStyle"
      v-loading="dataListLoading"
      ref="table"
      style="width: 100%;">
      <el-table-column align="center" label="受试者信息">
        <el-table-column
          sortable
          prop="studentName"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="110"
          label="学生姓名">
          <template slot-scope="scope">{{ scope.row.sportHistory.student.studentName }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="idNumber"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="110"
          label="身份证号">
          <template slot-scope="scope">{{ scope.row.sportHistory.student.idNumber }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="schoolName"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="110"
          label="学校">
          <template slot-scope="scope">{{ scope.row.sportHistory.student.schoolName }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="gradeName"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="100"
          label="班级">
          <template slot-scope="scope">{{ scope.row.sportHistory.student.gradeName }}{{ scope.row.sportHistory.student.className }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="sex"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="80"
          label="性别">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.sportHistory.student.sex == '1'">男</el-tag>
            <el-tag size="mini" v-if="scope.row.sportHistory.student.sex == '0'" type="danger">女</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          prop="age"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="80"
          label="年龄">
          <template slot-scope="scope">{{ scope.row.sportHistory.age }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="obesityDegree"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="130"
          label="肥胖程度">
          <template slot-scope="scope">{{ scope.row.sportHistory.obesityDegree }}</template>
        </el-table-column>
      </el-table-column>
      <el-table-column align="center" label="运动描述">
        <el-table-column
          sortable
          prop="sportModeA"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="运动形式1">
        </el-table-column>
        <el-table-column
          sortable
          prop="sportModeB"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="运动形式2">
        </el-table-column>
        <el-table-column
          sortable
          prop="sportModeC"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="运动形式3">
        </el-table-column>
        <el-table-column
          sortable
          prop="sportStrengthMin"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="170"
          label="运动强度（最低）">
        </el-table-column>
        <el-table-column
          sortable
          prop="sportStrengthMax"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="170"
          label="运动强度（最高）">
        </el-table-column>
        <el-table-column
          sortable
          prop="motionFre"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="运动频率">
          <template slot-scope="scope">{{ scope.row.sportHistory.motionFre }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="motionTime"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="运动时间">
          <template slot-scope="scope">{{ scope.row.sportHistory.motionTime }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="interventionCycle"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="干预周期">
          <template slot-scope="scope">{{ scope.row.sportHistory.interventionCycle }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="motionSumTime"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="总运动时间">
          <template slot-scope="scope">{{ scope.row.sportHistory.motionSumTime }}</template>
        </el-table-column>
      </el-table-column>
      <el-table-column align="center" label="结局指标（前测）">
        <el-table-column
          sortable
          prop="beforeWeight"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="体重 Kg">
          <template slot-scope="scope">{{ scope.row.sportHistory.beforeWeight }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="beforeWeightStd"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="体重标准差">
          <template slot-scope="scope">{{ scope.row.sportHistory.beforeWeightStd }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="beforeBmi"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="BMI Kg/m2">
          <template slot-scope="scope">{{ scope.row.sportHistory.beforeBmi }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="beforeBmiStd"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="BMI标准差">
          <template slot-scope="scope">{{ scope.row.sportHistory.beforeBmiStd }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="beforeFat"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="体脂百分比">
          <template slot-scope="scope">{{ scope.row.sportHistory.beforeFat }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="beforeFatStd"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="体脂标准差">
          <template slot-scope="scope">{{ scope.row.sportHistory.beforeFatStd }}</template>
        </el-table-column>
      </el-table-column>
      <el-table-column align="center" label="结局指标（后测）">
        <el-table-column
          sortable
          prop="afterWeight"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="体重 Kg">
          <template slot-scope="scope">{{ scope.row.sportHistory.afterWeight }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="afterWeightStd"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="体重标准差">
          <template slot-scope="scope">{{ scope.row.sportHistory.afterWeightStd }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="afterBmi"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="BMI Kg/m2">
          <template slot-scope="scope">{{ scope.row.sportHistory.afterBmi }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="afterBmiStd"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="BMI标准差">
          <template slot-scope="scope">{{ scope.row.sportHistory.afterBmiStd }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="afterFat"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="体脂百分比">
          <template slot-scope="scope">{{ scope.row.sportHistory.afterFat }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="afterFatStd"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="150"
          label="体脂标准差">
          <template slot-scope="scope">{{ scope.row.sportHistory.afterFatStd }}</template>
        </el-table-column>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        min-width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" style="padding:0;" v-if="isAuth('sport:sporthistorymiddle:update')" @click="updateHandle(scope.row)">修改</el-button>
          <el-button type="text" style="padding:0;" v-if="isAuth('sport:sporthistorymiddle:update')" @click="copyHandle(scope.row)">拷贝</el-button>
          <el-button type="text" style="padding:0;" v-if="isAuth('sport:sporthistorymiddle:delete')" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="queryParams.pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryParams.pageSize"
      :total="pageData.totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <edit :dialog-visible="dialog.isVisible" :type="dialog.type" @close="editClose" @success="editSuccess" ref="edit"></edit>
    <!-- 弹窗, excel导入 -->
    <excel-import v-if="excelImportVisible" ref="excelImport" @refreshDataList="getDataList(true)"></excel-import>
  </div>
</template>

<script>
  import edit from './sporthistorymiddle-edit'
  import { initQueryParams } from '@/utils'
  import { dateSelectShorts, formatMoney } from '@/utils/common'
  import excelImport from './excel-import-sport-middle'

  export default {
    data () {
      return {
        dialog: {
          isVisible: false,
          type: 'add'
        },
        tableHeight: this.$store.state.common.documentClientHeight - 260,
        queryParams: initQueryParams(),
        pageData: {
          totalCount: 0,
          list: [],
          extData: {}
        },
        dataListLoading: false,
        btnLoading: false,
        excelImportVisible: false,
        createTimePickerOptions: {
          shortcuts: dateSelectShorts()
        }
      }
    },
    components: { edit, excelImport },
    activated () {
      this.getDataList(true)
    },
    updated () {
      this.$nextTick(() => {
        this.$refs['table'].doLayout()
      })
    },
    methods: {
      editClose() {
        this.dialog.isVisible = false
      },
      editSuccess() {
        this.getDataList(true)
      },
      resetParams() {
        this.queryParams = initQueryParams()
        this.getDataList(true)
      },
      // 获取数据列表
      getDataList (isReload) {
        if (isReload) {
          this.queryParams.pageNo = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.requestUrl('/sport/sporthistorymiddle/list'),
          method: 'get',
          params: this.$http.requestParams(this.queryParams)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.pageData = data.page
          }
          this.dataListLoading = false
        })
      },
      // 弹出excel导入窗口
      excelImport () {
        this.excelImportVisible = true
        this.$nextTick(() => {
          this.$refs.excelImport.init()
        })
      },
      // 导出excel
      excelExport () {
        this.btnLoading = true
        this.$http({
          url: this.$http.requestUrl('/sport/sporthistorymiddle/exportExcel'),
          method: 'get',
          responseType: 'arraybuffer',
          params: this.$http.requestParams(this.queryParams)
        }).then(({data}) => {
          this.btnLoading = false
          let blob = new Blob([data])
          let fileName = '导出运动历史-中等强度持续训练.xlsx'
          // for IE
          if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(blob, fileName)
          } else { // for Non-IE (chrome, firefox ...)
            let downloadElement = document.createElement('a')
            let href = window.URL.createObjectURL(blob)
            downloadElement.href = href
            downloadElement.download = fileName
            document.body.appendChild(downloadElement)
            downloadElement.click()
            document.body.removeChild(downloadElement)
            window.URL.revokeObjectURL(href)
          }
        })
      },
      // 时间选择change事件
      dateChange (val) {
        this.queryParams.queryModel.createTime = val == null ? '' : val[0]
        this.queryParams.queryModel.createTimeEnd = val == null ? '' : val[1]
      },
      // 每页数
      sizeChangeHandle (val) {
        this.queryParams.pageSize = val
        this.queryParams.pageNo = 1
        this.getDataList(false)
      },
      // 当前页
      currentChangeHandle (val) {
        this.queryParams.pageNo = val
        this.getDataList(false)
      },
      // 排序
      sortChange: function (column) {
        if (column.order === 'descending') {
          this.queryParams.sort = column.prop + ' desc'
        } else if (column.order === 'ascending') {
          this.queryParams.sort = column.prop + ' asc'
        }
        this.getDataList(true)
      },
      // 新增
      addHandle () {
        this.dialog.type = 'add'
        this.dialog.isVisible = true
        this.$refs.edit.setDataForm(false)
      },
      // 修改
      updateHandle (row) {
        this.dialog.type = 'update'
        this.dialog.isVisible = true
        this.$refs.edit.setDataForm(row)
      },
      // 拷贝
      copyHandle (row) {
        this.dialog.type = 'copy'
        this.dialog.isVisible = true
        this.$refs.edit.setDataForm(row)
      },
      // 删除
      deleteHandle (id) {
        this.$confirm(`确定对id=${id}的记录进行删除?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.requestUrl('/sport/sporthistorymiddle/delete'),
            method: 'post',
            data: id
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList(true)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      headerCellStyle (row, rowIndex) {
        if (row.rowIndex === 0) {
          if(row.columnIndex === 1){
            return 'background:#82E0AA; color:#000; font-weight:bold; font-size:14px;'
          } else if(row.columnIndex === 2){
            return 'background:#FAD7A0; color:#000; font-weight:bold; font-size:14px;'
          } else if(row.columnIndex === 3){
            return 'background:#D2B4DE; color:#000; font-weight:bold; font-size:14px;'
          } else if(row.columnIndex === 4){
            return 'background:#DAF7A6; color:#000; font-weight:bold; font-size:14px;'
          } else {
            return 'background:#CEE3F6; color:#000; font-weight:bold; font-size:14px;'
          }
        } else if (row.rowIndex === 1) {
          if(row.columnIndex >=7 && row.columnIndex <=15){
            return 'background:#82E0AA; color:#000; font-weight:bold; font-size:14px;'
          } else if(row.columnIndex >=16 && row.columnIndex <=21){
            return 'background:#FAD7A0; color:#000; font-weight:bold; font-size:14px;'
          } else if(row.columnIndex >=22 && row.columnIndex <=27){
            return 'background:#D2B4DE; color:#000; font-weight:bold; font-size:14px;'
          } else {
            return 'background:#CEE3F6; color:#000; font-weight:bold; font-size:14px;'
          }
        } else {
          return 'background:#CEE3F6; color:#000; font-weight:bold; font-size:14px;'
        }
      }
    }
  }
</script>
